local sk__zhiji = fk.CreateSkill {

  name = "sk__zhiji",

  tags = {},

}



sk__zhiji:addEffect("active", {
  name = "sk__zhiji",
  anim_type = "offensive",
  min_card_num = 1,
  min_target_num = 1,
  prompt = "#sk__zhiji",
  can_use = function(self, player)
    return player:getMark("sk__zhiji-phase") == 0
  end,
  card_filter = function(self, player, to_select, selected)
    return Fk:getCardById(to_select).sub_type == Card.SubtypeWeapon and
        not player:prohibitDiscard(Fk:getCardById(to_select))
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected < #selected_cards and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    room:setPlayerMark(player, "sk__zhiji-phase", 1)
    room:sortByAction(effect.tos)
    room:throwCard(effect.cards, sk__zhiji.name, player, player)
    for _, target in ipairs(effect.tos) do
      if not target.dead then
        room:damage {
          from = player,
          to = target,
          damage = #effect.cards,
          skillName = sk__zhiji.name,
        }
      end
    end
  end,
})

sk__zhiji:addEffect(fk.EventPhaseStart, {
  name = "#sk__zhiji_trigger",
  mute = true,
  --sk__zhiji,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__zhiji.name) then
      return player.phase == Player.Start and player:isWounded()
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, "sk__zhiji", nil)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(room.draw_pile, function(id)
      return Fk:getCardById(id).sub_type == Card.SubtypeWeapon
    end)
    table.insertTable(cards, table.filter(room.discard_pile, function(id)
      return Fk:getCardById(id).sub_type == Card.SubtypeWeapon
    end))
    for _, p in ipairs(room.alive_players) do
      table.insertTable(cards, table.filter(p:getCardIds("e"), function(id)
        return Fk:getCardById(id).sub_type == Card.SubtypeWeapon
      end))
    end
    if #cards == 0 then return end
    room:moveCardTo(table.random(cards), Card.PlayerHand, player, fk.ReasonPrey, "sk__zhiji", nil, true, player)
    if player.dead or player:isKongcheng() then return end
    cards = table.filter(player:getCardIds("h"), function(id)
      return Fk:getCardById(id).type ~= Card.TypeEquip
    end)
    if #cards == 0 then return end
    room:askForDiscard(player, 1, 1, true, "sk__zhiji", false, ".|.|.|.|.|^equip", "#sk__zhiji-discard")
  end,
})
sk__zhiji:addEffect(fk.Damaged, {
  name = "#sk__zhiji_trigger",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__zhiji.name) then
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, "sk__zhiji", nil)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(room.draw_pile, function(id)
      return Fk:getCardById(id).sub_type == Card.SubtypeWeapon
    end)
    table.insertTable(cards, table.filter(room.discard_pile, function(id)
      return Fk:getCardById(id).sub_type == Card.SubtypeWeapon
    end))
    for _, p in ipairs(room.alive_players) do
      table.insertTable(cards, table.filter(p:getCardIds("e"), function(id)
        return Fk:getCardById(id).sub_type == Card.SubtypeWeapon
      end))
    end
    if #cards == 0 then return end
    room:moveCardTo(table.random(cards), Card.PlayerHand, player, fk.ReasonPrey, "sk__zhiji", nil, true, player)
    if player.dead or player:isKongcheng() then return end
    cards = table.filter(player:getCardIds("h"), function(id)
      return Fk:getCardById(id).type ~= Card.TypeEquip
    end)
    if #cards == 0 then return end
    room:askForDiscard(player, 1, 1, true, "sk__zhiji", false, ".|.|.|.|.|^equip", "#sk__zhiji-discard")
  end,
})

return sk__zhiji
